//
//  CXHAppDelegate.m
//  CXHAdSDK-GroMore
//
//  Created by 二跳 on 10/15/2024.
//  Copyright (c) 2024 二跳. All rights reserved.
//

#import "CXHAppDelegate.h"
#import <BUAdSDK/BUAdSDK.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>

@interface CXHAppDelegate ()<BUSplashAdDelegate>

@property (nonatomic, strong) BUSplashAd *splashAd;

@end

@implementation CXHAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self initBUSDK];
    // Override point for customization after application launch.
    return YES;
}

- (void)initBUSDK{
    BUAdSDKConfiguration *configuration = [BUAdSDKConfiguration configuration];
    // 如果使用聚合维度功能，则务必将以下字段设置为YES
    // 并检查工程有引用CSJMediation.framework，这样SDK初始化时将启动聚合相关必要组件
    configuration.useMediation = YES;
    configuration.appID = @"5596782";
//    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"CSJDemoResource" ofType:@"bundle"];
//    NSString *config = [[NSBundle bundleWithPath:bundlePath] pathForResource:@"GroMore-config-ios-5000546" ofType:@"json"];
//    configuration.mediation.advanceSDKConfigPath = config;
    configuration.debugLog = @(1);
    configuration.SDKDEBUG = YES;
    
//    NSLog(@"=======start init buadsdk=========");
    [BUAdSDKManager startWithAsyncCompletionHandler:^(BOOL success, NSError *error) {
//        NSLog(@"=======start init finish=========");
        if (!success) {
            NSLog(@"BUSDK start failed, %@", error.localizedDescription);
        } else {
//            [self loadSplash];
        }
    }];
}

- (void)loadSplash{
    CGSize size = kCXHScreenSize;
    BUSplashAd *splashAd = [[BUSplashAd alloc] initWithSlotID:@"103173549" adSize:size];
    _splashAd = splashAd;
    _splashAd.delegate = self;
    _splashAd.tolerateTimeout = 5;
    [_splashAd loadAdData];
}

#pragma mark - BUSplashAdDelegate
/// This method is called when material load successful
- (void)splashAdLoadSuccess:(BUSplashAd *)splashAd{
    [_splashAd showSplashViewInRootViewController:self.window.rootViewController];
}

/// This method is called when material load failed
- (void)splashAdLoadFail:(BUSplashAd *)splashAd error:(BUAdError *_Nullable)error{
    
}

/// This method is called when splash view render successful
// Mediation:/// @Note :  (针对聚合维度广告)<6300版本不会回调该方法，>=6300开始会回调该方法，但不代表最终展示广告的渲染结果。
- (void)splashAdRenderSuccess:(BUSplashAd *)splashAd{
    
}

- (void)splashAdRenderFail:(BUSplashAd *)splashAd error:(BUAdError *_Nullable)error{
    
}

/// This method is called when splash view will show
- (void)splashAdWillShow:(BUSplashAd *)splashAd{
    
}

/// This method is called when splash view did show
// Mediation:/// @Note :  当加载聚合维度广告时，支持该回调的adn有：CSJ
- (void)splashAdDidShow:(BUSplashAd *)splashAd{
    
}

/// This method is called when splash view is clicked.
- (void)splashAdDidClick:(BUSplashAd *)splashAd{
    
}

/// This method is called when splash view is closed.
- (void)splashAdDidClose:(BUSplashAd *)splashAd closeType:(BUSplashAdCloseType)closeType{
    
}

/// This method is called when splash viewControllr is closed.
// Mediation:/// @Note : 当加载聚合维度广告，最终展示的广告关闭时，
// Mediation:///         如该adn未提供“控制器关闭”回调，为保持逻辑完整，聚合逻辑内部在DidClose后补齐该回调，
// Mediation:///         如该adn提供“控制器关闭”回调，则以对应adn为准。
- (void)splashAdViewControllerDidClose:(BUSplashAd *)splashAd{
    
}

/**
 This method is called when another controller has been closed.
 @param interactionType : open appstore in app or open the webpage or view video ad details page.
 */
- (void)splashDidCloseOtherController:(BUSplashAd *)splashAd interactionType:(BUInteractionType)interactionType{
    
}

/// This method is called when when video ad play completed or an error occurred.
- (void)splashVideoAdDidPlayFinish:(BUSplashAd *)splashAd didFailWithError:(NSError *_Nullable)error{}


- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
